<?php


class gestionBD extends ModelePDO {


// 
// <editor-fold defaultstate="collapsed" desc="région Méthodes">

    /**
     * 
     * Retourne le prenom et le nom en un string.
     */

    /**
     * Retourne la liste de tous les membres
     * @return array() Tableau d'objets membres
     */
    // <editor-fold defaultstate="collapsed" desc="région Gestion Jeu">
    
    
    public static function VerifPersoByid($id)
    {
        self::seConnecter();
        $requete = "Select count(*)";
    }
    
    public static function getPersobyId($id)
    {
        self::seConnecter();
        $requete = "select count(*) as isLol from personnage,persolol where personnage.idPerso = persolol.idPersoL and idPerso = '" .$id."'";
        $pdoResults = self::$pdoCnxBase->prepare($requete);
        $pdoResults->execute();
        return $pdoResults->fetch(pdo::FETCH_OBJ)->isLol;
    }
    
    public static function getPersobyGame($id)
    {
        self::seConnecter();
        $requete = "select * from personnage,jeu where personnage.idJeu = jeu.idJeu and idJeu='" .$id."'";
        $pdoResults = self::$pdoCnxBase->prepare($requete);
        $pdoResults->execute();
        $lesPerso = $pdoResults->fetchAll(pdo::FETCH_CLASS);
        return $lesPerso;
    }
    
    public static function getGamebyName($name)
    {
        self::seConnecter();
        $requete = "Select * from jeu where NomJeu ='" .$name."'";
        $pdoResults = self::$pdoCnxBase->prepare($requete);
        $pdoResults->execute();
        $leJeu = $pdoResults->fetch(pdo::FETCH_OBJ);
        return $leJeu;
    }
    
    public static function getPersoByNameGame($name)
    {
        self::seConnecter();
        $requete = "Select * from personnage, jeu where Personnage.idJeu = Jeu.idJeu and nomJeu ='" .$name."'";
        $pdoResults = self::$pdoCnxBase->prepare($requete);
        $pdoResults ->execute();
        $lesPerso = $pdoResults->fetchAll(pdo::FETCH_OBJ);
        return $lesPerso;
    }
    
    public static function getPersobyId2($id)
    {
        self::seConnecter();
        $requete = "Select * from personnage where idPerso ='" .$id."'";
        $pdoResults = self::$pdoCnxBase->prepare($requete);
        $pdoResults->execute();
        $lePerso = $pdoResults->fetch(pdo::FETCH_OBJ);
        return $lePerso;
    }
    
     // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc="région Gestion Membre">

    
   
    
    public static function getLesMembres() {//Avec FETCH_OBJ et classe métier
        
        return self::getLesTuples('Membre');
        
    }

    public static function getLesMembres2() {//Avec FETCH_CLASS et classe métier
//Connexion à la base
        self::seConnecter();

        $requete = "SELECT * from Membre";

        $pdoStResults = self::$pdoCnxBase->prepare($requete);
        $pdoStResults->setFetchMode(PDO::FETCH_CLASS, 'Membre');
        $pdoStResults->execute();

        $lesMembres = $pdoStResults->fetchAll(PDO::FETCH_CLASS);

        return $lesMembres;
    }

    public static function getLesMembres3() {//Avec FETCH_OBJ et sans classe métier
//Connexion à la base
        self::seConnecter();

        $requete = "SELECT * from Membre";

       
        $pdoStResults = self::$pdoCnxBase->prepare($requete);

        $pdoStResults->execute();

        $lesMembres = $pdoStResults->fetchAll(PDO::FETCH_OBJ);

        return $lesMembres;
    }


    public static function getLesMembresById($id) {//Avec FETCH_OBJ et classe métier
//Connexion à la base
       return self::getPremierTupleByChamp('membre','id',$id );
    }

    public static function getLesMembresByLogin($login) {//Avec FETCH_OBJ et classe métier
//Connexion à la base
        //return self::getPremierTupleByChamp('membre','login',$login );
    }
    
    public static function getMembreByLogin($login){
        self::seConnecter();
        $requete = "Select * from Membre where pseudoMem = '" .$login . "'";
        
        $pdoStResults = self::$pdoCnxBase->prepare($requete);

        $pdoStResults->execute();

        $leMembre = $pdoStResults->fetch(PDO::FETCH_OBJ);
        
        return $leMembre;
        
        
    }

    public static function getNbMembres() {//Avec FETCH_OBJ et classe métier
        return (count(GestionBD::getLesMembres()));
    }

    public static function addMembres($login, $pass ) {

        self::seConnecter();

        $requete = 'INSERT INTO membre(login, pass_md5, Admin) VALUES("'.$login.'", "'.$pass.'","0")';

        $pdoResults = self::$pdoCnxBase->prepare($requete);

        $pdoResults->execute();
    }

    
    
    
    public static function setPasseMembre($id, $nouveauPasse) {

        self::seConnecter();

        $requete = "UPDATE membre SET passe ='" . $nouveauPasse . "' where id ='" . $id . "'";
        $pdoResults = self::$pdoCnxBase->prepare($requete);
        $pdoResults->execute();
    }

    public static function supprimerMembreById($id) {//Avec FETCH_OBJ et classe métier
        self::seConnecter();

        self::getPremierTupleByChamp('membre','id',$id );
    }
    
    // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc="région Verification de saisie">
    
    public static function verifierLoginPass($login, $pass) {
        self::seConnecter();

        $requete = 'SELECT count(*) As NbrCo FROM membre WHERE PseudoMem="' . $login . '" AND PassMembre="' . $pass.'"';
        $pdoResults = self::$pdoCnxBase->prepare($requete);
        $pdoResults->execute();
        return $pdoResults;
        
    }
    
        public static function verifierAdmin($login) {
        self::seConnecter();

        $requete = 'SELECT Admin FROM membre WHERE login="' . $login .'"';
        $pdoResults = self::$pdoCnxBase->prepare($requete);
        $pdoResults->execute();
        return $pdoResults->fetch(PDO::FETCH_OBJ)->Admin;
        
    }
    
        public static function verifStream($login){
            self::seConnecter();
            $requete = '';
        }
    
    public static function verifierStream($login){
        self::seConnecter();
        $requete ="select count(*) from Membre,stream where stream.idMembreS = membre.idMembre and PseudoMem = '" . $login . "'";
        $pdoResults = self::$pdoCnxBase->prepare($requete);
        $pdoResults->execute();
        return $pdoResults;
    }
    
    // </editor-fold>
  
    
    // <editor-fold defaultstate="collapsed" desc="région Gestion news">
     
     public static function ajouterNews($title, $contenue) {
        self::seConnecter();

        $requete = 'INSERT INTO news (Titre, Textenews) VALUES ("'.$title.'", "'.$contenue.'")';
        $pdoResults = self::$pdoCnxBase->prepare($requete);
        $pdoResults->execute();
        return $pdoResults;
        
    }
    
    public static function supprNews($id){
        self::seConnecter();
        $requete = "DELETE FROM news WHERE idNews = '" . $id . "'";
        $pdoResults = self::$pdoCnxBase->prepare($requete);
        $pdoResults->execute();
        return $pdoResults;
                }
    
    public static function supprimerTuplesById($table, $id) {
        self::seConnecter();

        switch ($table)
        { 
            case "produit": 
                $requete = "DELETE FROM Produit WHERE idProduit ='" . $id ."'";
            break;

            case "news": 
                $requete = "DELETE FROM news WHERE idNews ='" . $id ."'";
            break;

            default:

        }
        
        $pdoResults = self::$pdoCnxBase->prepare($requete);
        $pdoResults->execute();
        return $pdoResults; 
        
    }
    

    
    public static function getLesNews(){
        self::seConnecter();
       $requete = "SELECT * FROM news ORDER BY idNews DESC";
                $pdoStResults = self::$pdoCnxBase->prepare($requete);

        $pdoStResults->execute();

        $LesNews = $pdoStResults->fetchAll(PDO::FETCH_OBJ);
        
        return $LesNews;
    }
    
    public static function VerifNewsById($id){
        self::seConnecter();
        //$requete = "Select idNews from news,ecrite where news.idNews = ecrite.idNewsE and idNewsE = '" .$id."'" ;
        $requete = "Select count (*) from news,ecrite where news.idNews = ecrite.idNewsE and idNewsE = '" .$id. "'";
        var_dump($requete);
        $pdoStResults = self::$pdoCnxBase->prepare($requete);
        $pdoStResults->execute();
        
        return $pdoStResults->fetch(PDO::FETCH_OBJ)->idNews;
    }
        
        public static function VerifNewsById2($id){
         self::seConnecter();
         $requete = "Select idNews From news,video Where news.idNews = video.idNewsV and idNewsV= '" .$id ."'";
         //var_dump($requete);
         //$requete = "Select count (*) from news,video where news.idNews = video.idNewsE and idNewsE = '" .$id. "'";
         $pdoResults = self::$pdoCnxBase->prepare($requete);
        $pdoResults->execute();
        
        return $pdoResults->fetch(PDO::FETCH_OBJ)->idNews;
        }
        
    public static function VerifNewsById3($id){
        self::seConnecter();
        $requete = "Select count(*) as nbVideo From news,video Where news.idNews = video.idNewsV and idNewsV = '" .$id."'";
        
        $pdoResults = self::$pdoCnxBase->prepare($requete);
        $pdoResults->execute();
        //var_dump($pdoResults);
        return $pdoResults->fetch(pdo::FETCH_OBJ)->nbVideo;
        
    }
        
    
    
    public static function getVideobyId($id){
        self::seConnecter();
        $requete = "Select * from news,video where news.idNews = video.idNewsV and idNews = '". $id."' ";
        $pdoStResults = self::$pdoCnxBase->prepare($requete);
        $pdoStResults->execute();
        $laNewsVideo = $pdoStResults->fetch(PDO::FETCH_OBJ);
        return $laNewsVideo;
    }
    
    public static function getDetailbyID($id){
        self::seConnecter();
       $requete = "Select * from news,ecrite where news.idNews = ecrite.idNewsE and idNews = '" . $id ."'";
       $pdoStResults = self::$pdoCnxBase->prepare($requete);
       $pdoStResults->execute();
       $laNewsEcrit = $pdoStResults->fetch(PDO::FETCH_OBJ);
       return $laNewsEcrit;
    }
    
    public static function getLesVideos(){
        self::seConnecter();
        $requete = "Select * from news,video where news.idNews = video.idNewsV";
        $pdoStResults = self::$pdoCnxBase->prepare($requete);
        $pdoStResults->execute();

        $LesVideos = $pdoStResults->fetchAll(PDO::FETCH_OBJ);
        
        return $LesVideos;
    }
    
    public static function getLesEcrites(){
        self::seConnecter();
        $requete = "Select * from news,ecrite where news.idNews = ecrite.idNewsE";
        $pdoStResults = self::$pdoCnxBase->prepare($requete);
        $pdoStResults->execute();
        $lesEcrites = $pdoStResults->fetchAll(PDO::FETCH_OBJ);
        return $lesEcrites;
    }
    
//    public static function GetVideoById($id){
//        self::seConnecter();
//        $requete = "Select * from news,video where news.idNews = video.idNews and idNews = '" .$id. "'";
//        $pdoStResults = self::$pdoCnxBase->prepare($requete);
//        $pdoStResults->execute();
//        return $pdoStResults;
//    }
    

    // </editor-fold>
    
    



    // <editor-fold defaultstate="collapsed" desc="région Gestion Produit">
    public static function getLesProduits(){
        self::seConnecter();
            $requete = "SELECT * FROM Produit,Fournisseur,Categorie where Produit.Idproduit = fournisseur.IdFournisseur and produit.idCat = categorie.idCategorie ORDER BY IdProduit DESC LIMIT 0, 5";
   
        $pdoStResults = self::$pdoCnxBase->prepare($requete);

        $pdoStResults->execute();

        $LesProduits = $pdoStResults->fetchAll(PDO::FETCH_OBJ);

        return $LesProduits;
    }
    
    public static function getProduitById($idProduit) {
        return self::getPremierTupleByChamp('Produit', 'idProduit', $idProduit);
    }
    
    public static function addligneCommande($idCommande,$idProduit){
        self::seConnecter();
        $requete = "insert into lignecommande ('idProduit',idCommande,qteCommande) Values (".$idCommande.",".$idProduit.",1)";
        $pdoStResults = self::$pdoCnxBase->prepare($requete);
        $pdoStResults->execute();
    }
    
    public static function addCommande($idCommande,$idClient){
        self::seConnecter;
        $requete = "insert into commande ('idCommande',idMembre') Values (".$idCommande.",".$idClient.")";
        $pdoStResults = self::$pdoCnxBase->prepare($requete);
        $pdoStResults->execute();
    }
    
    // </editor-fold>

static function seConnecter() {
        if(is_null(self::$pdoCnxBase)) {  //pas encore de connexion
        try {            
            self::$pdoCnxBase = new PDO('mysql:host=' . self::$serveur . ';dbname=' . self::$base, self::$utilisateur, self::$passe);
            self::$pdoCnxBase->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            self::$pdoCnxBase->query("SET CHARACTER SET utf8");
        } 
        catch (Exception $e) {
            echo $e->getMessage();
        }
        
        }
    }

    public static function seDeconnecter() {
        
            self::$pdoCnxBase = null;
            //Si on n'appelle pas la méthode, la déconnexion a lieu en fin de script
                   
    }
    public static function getPremierTupleByChamp($table,$nomchamp,$valeurchamp){
        self::seConnecter();
        self::$requete = "Select distinct * From ".$table." Where ".$nomchamp."='".$valeurchamp."'";
        self::$pdoStResults = self::$pdoCnxBase->prepare(self::$requete);
        self::$pdoStResults->execute();
        self::$resultat = self::$pdoStResults->fetch(PDO::FETCH_OBJ);
        
        return self::$resultat;
    }
    
    // <editor-fold defaultstate="collapsed" desc="région Stream">
    
    public static function getStreamByLogin($login)
            {
        self::seConnecter();
        self::$requete = "Select * from membre,stream where membre.idMembre = stream.idMembreS and pseudoMem = '".$login."'";
        self::$pdoStResults = self::$pdoCnxBase->prepare(self::$requete);
        self::$pdoStResults->execute();
        $leStream = $pdoStResults->fetch(PDO::FETCH_OBJ);
        return $leStream;
        //return $pdoResults->fetch(pdo::FETCH_OBJ)->pseudoMem;
    }
    
    public static function getStream(){
        self::seConnecter();
        self::$requete = "Select * from membre,stream where membre.idMembre = stream.idMembreS";
        self::$pdoStResult = self::$pdoCnxBase->prepare(self::$requete);
        self::$pdoStResult->execute();
        $lesStreameurs = self::$pdoStResults->fetchAll(PDO::FETCH_OBJ);
        return $lesStreameurs;
    }
    
    public static function getStream2($id){
        self::seConnecter();
        $requete = "Select * from membre,stream where membre.idMembre = stream.idMembreS '". $id."' ";
        $pdoStResults = self::$pdoCnxBase->prepare($requete);
        $pdoStResults->execute();
        $leStream = $pdoStResults->fetch(PDO::FETCH_OBJ);
        return $leStream;
    }
    
    //</editor-fold>
    
    
    public static function modifLien($lien){
        self::seConnecter();
        $lien = $_POST['video1'];
        $lienRefont = str_replace('watch?v=', 'embed/', $lien);
        return $lienRefont;
        
    }
    
//    public static function addClient($leMembre, $rue, $codePostal, $ville) {
//
//        self::seConnecter();
//
//        $requete = "Insert into Client Values( " . $leMembre->id . ",'" . $rue . "','" . $codePostal . "','" . $ville . "')";
//
//        $pdoResults = self::$pdoCnxBase->prepare($requete);
//
//        $pdoResults->execute();
//    }
    

// </editor-fold>
}
